<template>
  <el-dialog
    :title="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible"
    class="jsbos-dialog jsbos-dialog_center"
    lock-scroll
    width="600px"
  >
    <el-row :gutter="15" class="">
      <el-form
        ref="elForm"
        :model="dataForm"
        :rules="rules"
        size="small"
        label-width="100px"
        label-position="right"
      >
        <template v-if="!loading">
          <el-col :span="24">
            <el-form-item label="投诉类型" prop="workOrderTypeId">
              <el-select
                v-model="dataForm.workOrderTypeId"
                placeholder="请选择"
                clearable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="(item, index) in workOrderTypeIdOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.enCode"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="投诉人" prop="contacts">
              <el-input
                v-model="dataForm.contacts"
                placeholder="请输入"
                @input="setTitle"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="投诉标题" prop="title">
              <el-input
                v-model="dataForm.title"
                placeholder="请输入"
                disabled
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="投诉电话" prop="contactsPhone">
              <el-input
                v-model="dataForm.contactsPhone"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="投诉科室" prop="location">
              <dep-select
                v-model="dataForm.location"
                placeholder="请选择"
                clearable
              >
              </dep-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="投诉方式" prop="putway">
              <el-select
                v-model="dataForm.putway"
                placeholder="请选择"
                clearable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="(item, index) in putwayOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.enCode"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="投诉时间" prop="complaintTime">
              <el-date-picker
                v-model="dataForm.complaintTime"
                placeholder="请选择"
                clearable
                :style="{ width: '100%' }"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="timestamp"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="优先级" prop="priority">
              <el-radio-group v-model="dataForm.priority" size="small">
                <el-radio
                  v-for="(item, index) in priorityOptions"
                  :key="index"
                  :label="item.enCode"
                  :disabled="item.disabled"
                >
                  {{ item.fullName }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="投诉内容描述" prop="note">
              <el-input
                v-model="dataForm.note"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="dataForm.remark"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="附件" prop="files">
              <UploadImg
                v-model="dataForm.files"
                :fileSize="5"
                sizeUnit="MB"
                :limit="9"
              >
              </UploadImg>
            </el-form-item>
          </el-col>
        </template>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取 消</el-button>
      <el-button
        :loading="submitLoad"
        type="primary"
        @click="dataFormSubmit('DRAFT')"
        >暂 存</el-button
      >
      <el-button
        :loading="submitLoad"
        type="primary"
        @click="dataFormSubmit('UNASSIGNED')"
        >提 交</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import request from "@/utils/request";
import { getDataInterfaceRes } from "@/api/systemData/dataInterface";

export default {
  components: {},
  props: [],
  data() {
    return {
      visible: false,
      loading: false,
      isDetail: false,
      dataForm: {
        workOrderTypeId: "",
        contacts: "",
        contactsPhone: "",
        location: "",
        putway: "",
        complaintTime: "",
        priority: "",
        note: "",
        remark: "",
        files: [],
      },
      rules: {
        workOrderTypeId: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
        location: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
        contacts: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        contactsPhone: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
          {
            pattern: /^1[3-9]\d{9}$/, // 手机号正则表达式
            message: "请输入有效的手机号",
            trigger: ["blur", "change"],
          },
        ],
      },
      workOrderTypeIdOptions: [],
      putwayOptions: [],
      priorityOptions: [],
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getWorkOrderTypeIdOptions();
    this.getPriorityOptions();
    this.getPutWayOptionsOptions();
  },
  mounted() {},
  methods: {
    setTitle(value) {
      let selectedOption = this.workOrderTypeIdOptions.find(
        (option) => option.enCode === this.dataForm.workOrderTypeId
      );
      console.log("---", selectedOption);
      this.dataForm.title =
        value + "提交" + selectedOption.fullName + "的投诉工单";
    },
    getWorkOrderTypeIdOptions() {
      this.$store
        .dispatch("base/getDictionaryData", {
          encode: "complaint",
        })
        .then((res) => {
          this.workOrderTypeIdOptions = res;
        });
    },
    getPriorityOptions() {
      this.$store
        .dispatch("base/getDictionaryData", {
          encode: "priority",
        })
        .then((res) => {
          this.priorityOptions = res;
        });
    },
    getPutWayOptionsOptions() {
      this.$store
        .dispatch("base/getDictionaryData", {
          encode: "complaintWay",
        })
        .then((res) => {
          this.putwayOptions = res;
        });
    },
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
    },
    init(id, isDetail) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.isDetail = isDetail || false;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          request({
            url: "/api/questionnaire/OqComplaintWorkOrder/" + this.dataForm.id,
            method: "get",
          }).then((res) => {
            this.dataInfo(res.data);
            this.loading = false;
          });
        } else {
          this.clearData(this.dataForm);
          this, (this.dataForm.priority = "urgent");
        }
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    // 表单提交
    dataFormSubmit(enabledMark) {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          this.request(enabledMark);
        }
      });
    },
    request(enabledMark) {
      var _data = this.dataList();
      _data.enabledMark = enabledMark;
      this.submitLoad = true;
      if (!this.dataForm.id) {
        request({
          url: "/api/questionnaire/OqComplaintWorkOrder",
          method: "post",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      } else {
        request({
          url: "/api/questionnaire/OqComplaintWorkOrder/" + this.dataForm.id,
          method: "PUT",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      }
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      _data.files = JSON.stringify(_data.files);
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      _dataAll.files = JSON.parse(_dataAll.files);
      this.dataForm = _dataAll;
    },
  },
};
</script>
